#import "TagSourceController.h"

@implementation TagSourceController


- (int)numberOfTags{
	return [sourceTags count];
}

-(NSMutableArray*) returnTargetTags{

	return targetTags;

}


- (void) addSourceTag: (NSString *) theString{

	[sourceTags addObject:theString];
	[sourceTagView reloadData];

}





- (void)awakeFromNib
{
	sourceTags = [[NSMutableArray alloc] init];
	targetTags = [[NSMutableArray alloc] init];
	

	[sourceTagView reloadData];
	[targetTagView reloadData];
	
	[sourceTagView registerForDraggedTypes: [NSArray arrayWithObject:@"NSString"] ];
	[targetTagView registerForDraggedTypes: [NSArray arrayWithObject:@"NSString"] ];
	
	
	
	printf("Awaking from NIB");
}


- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
    // Copy the row numbers to the pasteboard.
	
	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
    [pboard declareTypes:[NSArray arrayWithObject:@"NSString"] owner:self];
    [pboard setData:data forType:@"NSString"];
    return YES;
}


- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
			  row:(int)row dropOperation:(NSTableViewDropOperation)operation
{
	
	
    NSPasteboard* pboard = [info draggingPasteboard];
    NSData* rowData = [pboard dataForType:@"NSString"];
    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
    int dragRow = [rowIndexes firstIndex];
	
	
	if(aTableView == targetTagView){
		
		printf("Target tags");
		
		
		
		//	[targetTags addObject:[sourceTags objectAtIndex:dragRow]];
		
		
		
		[targetTags insertObject:[sourceTags objectAtIndex:dragRow] atIndex:row];
		
		[sourceTags removeObjectAtIndex:dragRow];
		
	}
    
	
	if(aTableView == sourceTagView){
		printf("Source tags");
		[sourceTags insertObject:[targetTags objectAtIndex:dragRow] atIndex:row];
		
		
		//[sourceTags addObject:[targetTags objectAtIndex:dragRow]];
		
		[targetTags removeObjectAtIndex:dragRow];
		
	}
	
	
	[sourceTagView reloadData];
	[targetTagView reloadData];
	
	return YES;
}


- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
{
    NSLog(@"validate Drop");
    return NSDragOperationEvery;
}



- (IBAction)buttonPressed: (id)aSender{
	[sourceTags addObject: @"foo" ];
	//[targetTags addObject: @"ffdfoo" ];
	
	[sourceTagView reloadData];
	[targetTagView reloadData];
	
	
}


- (id)     tableView:(NSTableView *) aTableView
objectValueForTableColumn:(NSTableColumn *) aTableColumn
				 row:(int) rowIndex
{  
	if(aTableView == targetTagView){
		return [targetTags objectAtIndex:rowIndex];  
		
	}
	
	if(aTableView == sourceTagView){
		return [sourceTags objectAtIndex:rowIndex];  
		
		
	}
	
	
	return nil;
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
	
	if(aTableView == targetTagView){
		return [targetTags count];  
	}
	
	if(aTableView == sourceTagView){
		return [sourceTags count];  
	}
	
	
	return 0;
	
}

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
	
}



@end
